<?php
/**
 * 竞猜答题类
 */
namespace app\admin\controller;

use app\admin\business\AnswerBus;
use app\admin\business\GoodsBus;
use app\admin\model\Goods as goodsModel;
use common\Tree;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Answer extends Common
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 商品列表
     * @author lijia
     * @date 2019-08-08
     */
    public function index()
    {
        if (Request::isPost()) {
            $key = input('post.key');
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $sort_by = input('sort_by') ? input('sort_by') : 'id';
            $sort_order = input('sort_order') ? input('sort_order') : 'asc';
            $where = [];
            if ($key) {
                $where[] = ['title', 'like', "%" . $key . "%"];
            }
            $list = AnswerBus::getList($where,$sort_by,$sort_order,$pageSize,$page);
            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }
        return View::fetch();
    }


    /**
     * 添加商品
     * @author lijia
     * @date 2019-08-08
     */
    public function add()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                $data['create_time'] = date('Y-m-d H:i:s',time());
                if (isset($data['qu'])){
                    $data['qus'] = json_encode($data['qu']);
                } else {
                    $data['qus'] = '';
                }
                unset($data['qu']);
                Db::name('qa')->insert($data);
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                $qus = [];
                $this->assign('qus', $qus);
                return View::fetch('edit');
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    /**
     * 编辑商品
     * @author lijia
     * @date 2019-08-08
     */
    public function edit()
    {
        try {
            if (request()->isPost()) {
                $data = input('post.');
                if (isset($data['qu'])){
                    $data['qus'] = json_encode($data['qu']);
                } else {
                    $data['qus'] = '';
                }
                unset($data['qu']);
                Db::name('qa')->where('id',$data['id'])->update($data);
                return ['code' => 0, 'msg' => '操作成功!'];
            } else {
                $id = input('id', 0, 'intval');
                if (!$id) {
                    return ['code' => 1, 'msg' => '非法请求!'];
                }
                $info = Db::name('qa')->find($id);
                if ($info['qus'] == '' || $info['qus'] == null){
                    $qus = [];
                }else{
                    $qus = json_decode($info['qus'], true);
                }
                $this->assign('qus', $qus);
                $this->assign('info', $info);
                return View::fetch();
            }
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    public function doDelete()
    {
        try {
            $id = input('request.id');
            if (!$id) {
                return ['code' => 3000, 'msg' => '请选择要删除的数据!'];
            }
            $type = Db::name('goods')->where("id", $id)->value('type');
            if ($type == 2){
                return ['code' => 3000, 'msg' => '礼包不能删除!'];
            }
            $result = Db::name('goods')->where("id", $id)->delete();
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


    public function doStatus()
    {
        try {
            $id = input('request.id');
            $status = input('request.status');
            if (!$id) {
                return ['code' => 3000, 'msg' => '操作失败!'];
            }
            $result = Db::name('goods')->where("id", $id)->update(['status' => $status]);
            if ($result) {
                return ['code' => 0, 'msg' => '操作成功!'];
            }
            return ['code' => 3000, 'msg' => '操作失败!'];
        } catch (\Exception $e) {
            return ['code' => 99999, 'msg' => $e->getMessage()];
        }
    }


}